import { defineStore } from 'pinia';
import { to } from 'await-to-js';
import { queryDeptCount } from '@/api/basic/flowable/index.ts'

interface NoticeItem {
  title?: string;
  read: boolean;
  message: any;
  time: string;
  waitCount: number;
  readCount: number;
}

export const useNoticeStore = defineStore('notice', () => {


  const waitCount = ref(0);
  const readCount = ref(0);


  const state = reactive({
    notices: [] as NoticeItem[]
  });

  const addNotice = (notice: NoticeItem) => {
    state.notices.push(notice);
  };

  const removeNotice = (notice: NoticeItem) => {
    state.notices.splice(state.notices.indexOf(notice), 1);
  };

  //实现全部已读
  const readAll = () => {
    state.notices.forEach((item) => {
      item.read = true;
    });
  };

  const QueryDeptCount = async () => {
    const [err, res] = await to(queryDeptCount({}));
    if (res) {
      waitCount.value = Number(res?.data?.waitCount || 0)
      readCount.value = Number(res?.data?.readCount || 0)
      return Promise.resolve();
    }
    return Promise.reject(err);
  };

  const clearNotice = () => {
    state.notices = [];
  };
  return {
    state,
    waitCount,
    readCount,
    addNotice,
    removeNotice,
    readAll,
    clearNotice,
    QueryDeptCount
  };
});

export default useNoticeStore;
